//
//  ViewController.m
//  图片细节
//
//  Created by 小果 on 16/5/13.
//  Copyright © 2016年 小果. All rights reserved.
//

#import "ViewController.h"
#define ScreenW [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
@property (nonatomic, strong) NSArray *images;
@property (nonatomic, assign) int currImage;
@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property (nonatomic, assign) CGFloat alpha;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.currImage = 0;
    self.alpha = 1.0;
    
    self.images = [NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg", @"7.jpg",nil];
    
    // 启用image1的用户交互，从而允许该控件能够响应用户的手势
    self.image1.userInteractionEnabled = YES;
    self.image1.layer.borderWidth = 3;
    self.image1.layer.borderColor = [UIColor redColor].CGColor;
    self.image1.layer.cornerRadius = 10;
    self.image1.layer.masksToBounds = YES;
    
    self.image2.layer.borderWidth = 3;
    self.image2.layer.borderColor = [UIColor redColor].CGColor;
    self.image2.layer.cornerRadius = 10;
    self.image2.layer.masksToBounds = YES;
    
    // 创建一个手势
    UITapGestureRecognizer *singTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicked:)];
    // 为image1添加一个手势
    [self.image1 addGestureRecognizer:singTap];
    
    self.image1.image = [UIImage imageNamed:[self.images objectAtIndex:0]];
}

- (IBAction)plus:(id)sender {
    self.alpha += 0.02;
    // 如果透明度已经大于或等于1.0，就将透明度设置为1.0
    if (self.alpha >= 1.0){
        self.alpha = 1.0;
    }
    // 设置image1的透明度
    self.image1.alpha = self.alpha;
}
- (IBAction)minus:(id)sender {
    self.alpha -= 0.02;
    // 如果透明度已经小于或等于0.0，就将透明度设置为0.0
    if(self.alpha <= 0.0){
        self.alpha = 0.0;
    }
    // 设置image的透明度
    self.image1.alpha = self.alpha;
}
- (IBAction)next:(id)sender {
    // 控制image1中的图片显示为images数组中的下一张图片
    self.image1.image = [UIImage imageNamed:[self.images objectAtIndex:(++self.currImage % self.images.count)]];
}

-(void)clicked:(UIGestureRecognizer *)gest{
    
    // 获取正在显示的原始位图
    UIImage *srcImage = self.image1.image;
    // 获取用户手指在image1上的触碰点
    CGPoint pt = [gest locationInView:self.image1];
    // 获取正在显示的原图对应的CGImageRef
    CGImageRef sourceImage = [srcImage CGImage];
    // 获取图片实际大小与第一个UIImageView的缩放比例
    CGFloat scale = srcImage.size.width / ScreenW;
    // 将image1上的触碰点的左边换算成原始图片上的位置
    CGFloat X = pt.x * scale;
    CGFloat Y = pt.y * scale;
    if (X + 120 > srcImage.size.width){
        
        X = srcImage.size.width - 140;
    }
    if (Y + 120 > srcImage.size.height){
        
        Y = srcImage.size.height - 140;
    }
    // 调用CGImageCreateWithImageInRect函数来获取sourceImage中指定区域的图片
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImage, CGRectMake(X, Y, 140, 140));
    // 让image2控件显示为newImageRef对应的图片
    self.image2.image = [UIImage imageWithCGImage:newImageRef];
    
}

@end
